package com.xyxe.domain.strategy.service.rule.tree.impl;


import com.xyxe.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.xyxe.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.xyxe.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {

    private Long userRaffleCount = 0L;

    @Override
    public DefaultTreeFactory.TreeActionEntity  logic(String userId, Long strategyId, Integer awardId, String ruleValue) {
        log.info("规则过滤-次数锁 userId:{} strategyId:{} awardId:{} ", userId, strategyId, awardId);

        long raffleCount;

        try {
            raffleCount = Long.parseLong(ruleValue);
        } catch (NumberFormatException e) {
            throw new RuntimeException("规则过滤-次数锁异常 ruleValue:" + ruleValue + "配置不正确");
        }

        // 用户抽奖次数大于等于规定次数，规则放行
        if (raffleCount <= userRaffleCount) {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.ALLOW)
                    .build();
        }

        // 用户抽奖次数小于规定次数，规则拦截
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
